package io.renren.modules.sell.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sell.dao.OrderDetailDao;
import io.renren.modules.sell.dao.SellerOrderDao;
import io.renren.modules.sell.entity.OrderDetail;
import io.renren.modules.sell.entity.OrderMaster;
import io.renren.modules.sell.response.OrderResponse;
import io.renren.modules.sell.service.OrderService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderServiceImpl
 * @Author mengjiafeng
 * @Date 2020/4/5 20:13
 **/
@Service
public class OrderServiceImpl extends ServiceImpl<SellerOrderDao, OrderMaster> implements OrderService {
    @Autowired
    OrderDetailDao orderDetailDao;
    @Override
    public PageUtils queryPageByUserId(Map<String, Object> params) {
        Long createUserId = (Long) params.get("createUserId");
        String buyername = (String)params.get("buyername");
        String orderid = (String)params.get("orderid");
        IPage<OrderMaster> page = this.page(
                new Query<OrderMaster>().getPage(params),
                new QueryWrapper<OrderMaster>()
                        .eq(createUserId != null, "user_id", createUserId)
                        .like(StringUtils.isNotBlank(buyername),"buyer_name",buyername)
                        .eq(StringUtils.isNotBlank(orderid),"order_id",orderid)
        );
        List<OrderMaster> records = page.getRecords();
        List<String> orderIdList = new ArrayList<>();
        for (OrderMaster record : records) {
            orderIdList.add(record.getOrderId());
        }
        ArrayList<OrderResponse> orderResponses = new ArrayList<>();
        List<OrderDetail> orderDetailList = orderDetailDao.getOrderList(orderIdList);
        //将javabean转换成返回类型
        for (OrderMaster record : records) {
            OrderResponse orderResponse = new OrderResponse();
            BeanUtils.copyProperties(record, orderResponse);
            orderResponse.setOrderDetailList(new ArrayList<>());
            orderResponses.add(orderResponse);
        }

        //组装返回类型，将详情装入list
        for (OrderResponse orderRespons : orderResponses) {
            for (OrderDetail orderDetail : orderDetailList) {
                if (orderRespons.getOrderId().equals(orderDetail.getOrderId())) {
                    orderRespons.getOrderDetailList().add(orderDetail);
                }
            }

        }
        return new PageUtils(orderResponses,(int)page.getTotal(),(int)page.getSize(),(int)page.getCurrent());
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String buyername = (String)params.get("buyername");
        String orderid = (String)params.get("orderid");

        IPage<OrderMaster> page = this.page(
                new Query<OrderMaster>().getPage(params),
                new QueryWrapper<OrderMaster>()
                        .like(StringUtils.isNotBlank(buyername),"buyer_name",buyername)
                        .eq(StringUtils.isNotBlank(orderid),"order_id",orderid)
        );
        List<OrderMaster> records = page.getRecords();
        List<String> orderIdList = new ArrayList<>();
        for (OrderMaster record : records) {
            orderIdList.add(record.getOrderId());
        }
        ArrayList<OrderResponse> orderResponses = new ArrayList<>();
        List<OrderDetail> orderDetailList = orderDetailDao.getOrderList(orderIdList);
        //将javabean转换成返回类型
        for (OrderMaster record : records) {
            OrderResponse orderResponse = new OrderResponse();
            BeanUtils.copyProperties(record, orderResponse);
            orderResponse.setOrderDetailList(new ArrayList<>());
            orderResponses.add(orderResponse);
        }

        //组装返回类型，将详情装入list
        for (OrderResponse orderRespons : orderResponses) {
            for (OrderDetail orderDetail : orderDetailList) {
                if (orderRespons.getOrderId().equals(orderDetail.getOrderId())) {
                    orderRespons.getOrderDetailList().add(orderDetail);
                }
            }

        }
        return new PageUtils(orderResponses,(int)page.getTotal(),(int)page.getSize(),(int)page.getCurrent());
    }

    @Override
    public void deleteBatch(Long[] userIds) {
        this.removeByIds(Arrays.asList(userIds));
    }
}
